# Built-In Example AI

# Title: donfeng
# Author: Wei Junyi AND Cheng Yujie
# Version: 1.0.0
# Date: 11/13/2020

import game.tcturtle
import math

class CombatTurtle(game.tcturtle.TurtleParent):
    """Turret combat turtle.

    Stays completely while turning to face the opponent turtle, shooting when
    within range.
    """

    #-------------------------------------------------------------------------

    def class_name():
        """CombatTurtle.class_name() -> str
        Static method to return the name of the Combat Turtle AI.
        """
        #我们把AI的名字命名为"Donfeng's AI"
        return "ct_donfeng"

    #-------------------------------------------------------------------------

    def class_desc():
        """CombatTurtle.class_desc() -> str
        Static method to return a description of the Combat Turtle AI.
        """
        #这是我们对于AI的介绍和说明
        return "Stays still and shoots opponent and back when close enough."

    #-------------------------------------------------------------------------

    def class_shape():
        """CombatTurtle.class_shape() -> (int or tuple)
        Static method to define the Combat Turtle's shape image.

        The return value can be either an integer or a tuple of tuples.

        Returning an integer index selects one of the following preset shapes:
            0 -- arrowhead (also default in case of unrecognized index)
            1 -- turtle
            2 -- plow
            3 -- triangle
            4 -- kite
            5 -- pentagon
            6 -- hexagon
            7 -- star

        A custom shape can be defined by returning a tuple of the form
        (radius, angle), where radius is a tuple of radii and angle is a tuple
        of angles (in radians) describing the polar coordinates of a polygon's
        vertices. The shape coordinates should be given for a turtle facing
        east.
        """



        #这部分是坦克造型的代码（已注释)

        """
        r1 = 4*math.sqrt(2)
        r2 = 16
        r3 = 8/math.sqrt(3)
        #r2 = r1/math.sqrt(2)
        radius = (r2, r3, r1, 2*r1, 2*r1, r1, r1, 2*r1, 2*r1, r1, r3)
        t1 = math.pi/4 #45度
        t2 = math.pi/6 #30度
        angle = (0, t2, t1, t1, 3*t1, 3*t1, 5*t1, 5*t1, -t1, -t1, -t2)


        return (radius,angle)
        """

        #比赛中我们选择箭头作为造型
        return 0
    #=========================================================================

    def setup(self):
        """CombatTurtle.setup() -> None
        Initialization code for Combat Turtle.
        """

        pass

    #-------------------------------------------------------------------------

    def step(self):
        """CombatTurtle.setup() -> None
        Step event code for Combat Turtle.
        """

        # Turn towards opponent
        self.turn_towards()


        if self.distance() > 4 * self.missile_radius:
            self.backward()


        # Shoot if facing opponent, there is line of sight, and close enough
        #这里我们把攻击时机设置为敌人接近1.5倍射程时，先敌发现，先敌开火
        #如果敌人接近到5倍攻击距离，我们会全速后退
        if self.can_shoot and abs(self.relative_heading_towards()) <= 5 and self.line_of_sight() and self.distance() <= self.missile_range*1.5:
            self.shoot()

